package com.miao.test.support;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReflectUtil;
import com.google.protobuf.Message;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/20 20:10
 * @Description:
 */
public class ProtoBufUtil {

    /**
     * @Description: 转换proto实体类
     * @Author: miaoguoxin
     * @Date: 2021/3/21
     */
    @SuppressWarnings("all")
    public static <T extends Message> T parse(byte[] data, Class<T> type) {
        Assert.notNull(data);
        Assert.notNull(type);
        Method method = ReflectUtil.getMethod(type, "parseFrom", byte[].class);
        try {
            return (T) method.invoke(null, data);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

}
